<?

include_once 'transport.php';
include_once '../inc/functions.php';

function cleanData(&$str) {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if (strstr($str, '"'))
        $str = '"' . str_replace('"', '""', $str) . '"';
}

$filename = "Extrato_" . date('d/m/Y') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel;  charset=UTF-8");
echo "\t\t\tExtrato " . date('d/m/Y - H:i:s') . " \t\r\n\r\n";
$flag = false;
foreach ($_SESSION[xml] as $row) {
    if (!$flag) {
        // display field/column names as first row
        echo "Entradas\t";
        echo "Saídas\t";
        echo "Subtotal\t";
        echo "Descrição\t";
        echo "Categoria\t";
        echo "Responsável\t";
        echo "Data\t";
        echo "Hora \r\n";
//        echo implode("\t", array_keys($row)) . "\r\n";
        $flag = true;
    }
    array_walk($row, 'cleanData');
    if (array_key_exists("entrada", $row)) {
        echo HTML_to_UTF8_acentos(implode("\t", array_values($row)) . "\r\n");
    }
}
exit;
?>